package io.gitee.zhangbinhub.acp.core.common.security

import io.gitee.zhangbinhub.acp.core.common.base.BaseEncrypt
import io.gitee.zhangbinhub.acp.core.common.log.LogFactory
import java.security.MessageDigest

/**
 * @author zhang by 10/07/2019
 * @since JDK 11
 */
object SignatureEncrypt : BaseEncrypt() {

    private val log = LogFactory.getInstance(SignatureEncrypt::class.java)

    internal var MD5 = "MD5"

    internal var SHA1 = "SHA-1"

    internal var SHA256 = "SHA-256"

    /**
     * 加密
     *
     * @param plainText 待加密字符串
     * @param algorithm 使用的算法
     * @return 密文
     */
    @JvmStatic
    @JvmOverloads
    fun encrypt(
        plainText: String, algorithm: String,
        inputEncode: ByteEncodeEnum = ByteEncodeEnum.BYTE,
        outputEncode: ByteEncodeEnum = ByteEncodeEnum.HEX
    ): String = try {
        encodeToString(MessageDigest.getInstance(algorithm).digest(decodeToBytes(plainText, inputEncode)), outputEncode)
    } catch (e: Exception) {
        log.error(e.message, e)
        ""
    }

}
